//获得传入的?key=.value 参数
function getValue(key) {
	var str = decodeURI(window.location.search); //location.search是从当前URL的?号开始的字符串  
	var value = "";
	if(str.indexOf(key) != -1) {
		var pos_start = str.indexOf(key) + key.length + 1;
		var pos_end = str.indexOf("&", pos_start);
		if(pos_end == -1) {
			value = str.substring(pos_start);
		} else {
			value = str.substring(pos_start, pos_end);
		}
	}
	return value;
}
function baseResURL(){
	return "http://sensor.sureserve.cn/";
}

function baseURL(){
//	var URLIP = 'http://124.232.165.110:38080/interfaces';			//测试服务器
	var URLIP = 'http://sensor.sureserve.cn/interfaces';		//正式服务器
	return URLIP;
}
function newBaseUrl() {
	var URLIP = 'http://124.232.165.93:34881';
	return URLIP;
}
/*function windDirection(tmp) {
		var direction;
		console.log(tmp + "风向");
		if(tmp == 0){
			direction = "暂无风向信息";
		}else if(tmp > 67.5 && tmp <= 112.5){
			direction = "东";
		}else if(tmp > 112.5 && tmp <= 157.5){
			direction = "东南";
		}else if(tmp > 157.5 && tmp <= 202.5){
			direction = "南";
		}else if(tmp > 202.5 && tmp <= 247.5){
			direction = "西南";
		}else if(tmp > 247.5 && tmp <= 292.5){
			direction = "西";
		}else if(tmp > 292.5 && tmp <= 337.5){
			direction = "西北";
		}else if(tmp > 337.5 && tmp <= 22.5){
			direction = "北";
		}else if(tmp > 22.5 && tmp <= 67.5){
			direction = "东北";
		}else{
			direction = tmp;
		}
		return direction;
}*/

//======================扩展js格式化日期======================
// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

//判断是否为空
function isNull(object) {
	if(!object || object == null || object == "" || object == "undefined" || object == 'null' || object == 'Null') {
		return true;
	} else {
		return false;
	}
}

function format(object, def) {
	if(!object || object == null || object == "" || object == "undefined" || object == 'null' || object == 'Null') {
		return def || "";
	} else {
		return object;
	}
}

function getFloat(val, length) {
	var tmp = val + " ";
	if(tmp.indexOf(".") > -1) {
		return tmp.substr(0, tmp.indexOf(".") + length + 1);
	} else {
		return tmp;
	}
}

function getNowYear() {
	var day2 = new Date();
	day2.setTime(day2.getTime());
	return day2.getFullYear();
}

function getNowMonth() {
	var day2 = new Date();
	day2.setTime(day2.getTime());
	return day2.getMonth() + 1;
}

function dateFtt(fmt, date) { //author: meizz   
	var o = {
		"M+": date.getMonth() + 1, //月份   
		"d+": date.getDate(), //日   
		"h+": date.getHours(), //小时   
		"m+": date.getMinutes(), //分   
		"s+": date.getSeconds(), //秒   
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度   
		"S": date.getMilliseconds() //毫秒   
	};
	if(/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

////沉浸状态栏
////hui.immersedStatusbar();
//hui.plusReady(function() {
//	//判断是否支持沉浸式
//	//var isImmersedStatusbar = plus.navigator.isImmersedStatusbar();
//	//console.log(isImmersedStatusbar);
//	//获取系统状态栏高度
//	var StatusbarHeight = plus.navigator.getStatusbarHeight();
//
//	console.log("StatusbarHeight:" + StatusbarHeight);
//
//	if(hui('.hui-header')) {
//		hui('.hui-header').css({
//			'paddingTop': StatusbarHeight + 'px'
//		});
//	}
//
//	if(hui('#hui-back')) {
//		hui('#hui-back').css({
//			top: StatusbarHeight + 'px'
//		});
//	}
//
//	if(hui('.hui-wrap')) {
//		hui('.hui-wrap').eq(0).css({
//			'paddingTop': (StatusbarHeight) + 'px'
//		});
//	}
//
//	//	hui('.wrap').eq(0).css({
//	//		'paddingTop': StatusbarHeight + 'px'
//	//	});
//
//});

function postJson(url, param, sucess, err) {
	hui.postJSON(url, param, function(ret) {
		console.log(JSON.stringify(ret));
		hui.closeLoading();

		if(ret && ret.code == 0) {
			sucess(ret);
		} else {
			var msg = ret.msg || "网络错误";
			console.log(JSON.stringify(ret))
			hui.toast(msg);
			if(err) {
				err(msg);
			}
		}
	}, function(e) {
		//console.log(e);
		hui.closeLoading();

		console.log(JSON.stringify(e));
		hui.upToast(e);
		if(err) {
			err(e);
		}
	});
}

function postByJson(url, param, sucess, err) {
	hui.postByJSON(url, param, function(ret) {
		//console.log(JSON.stringify(ret));
		hui.closeLoading();

		if(ret && ret.result == 0) {
			sucess(ret);
		} else {
			var msg = ret.msg || "网络错误";
			console.log(JSON.stringify(ret))
			hui.toast(msg);
			if(err) {
				err(msg);
			}
		}
	}, function(e) {
		//console.log(e);
		hui.closeLoading();

		console.log(JSON.stringify(e));
		hui.upToast(e);
		if(err) {
			err(e);
		}
	});
}

function printLog(value, title) {
	//console.log("-----------" + title + "-------------");
	//console.log(JSON.stringify(value));
}

function getUserId() {
	return localStorage.getItem("loginId") || "1";
}

function getUserInfo() {
	var info = localStorage.getItem("loginInfo") || "{}";
	//printLog(JSON.parse(info),"获取用户信息");
	var userInfo = JSON.parse(info)
	return userInfo;
}

var TargetTypes = ["空气温度", "空气湿度", "土壤温度", "土壤湿度", "土壤电导率", "土壤PH","风速", "风向", "光照度", "负氧离子浓度"
,"雨量","紫外辐射","大气压","光合辐射","日照时数","空气二氧化碳","盐分","水溶解氧","水溶解二氧化碳"];


var TargetIcons = ['img/AirTemperature-s.png',
	'img/AirHumidity-n.png',
	'img/LandTemperature-s.png',
	'img/LandHumidity-n.png',
	'img/LandEc-n.png',
	'img/LandPh-n.png',
	'img/WindSpeed-n.png',
	'img/WindDirect-s.png',
	'img/Light-s.png',
	'img/DissolvedOxygen-n.png',
	'img/RainFall-n.png',
	'img/UltravioletRadiation-n.png',
	'img/AtmosphericPressure-s.png',
	'img/PhotosyntheticRadiation-n.png',
	'img/LightHour-n.png',
	'img/AirCo2-s.png',
	'img/Salinity-n.png',
	'img/DissolvedOxygen-n.png',
	'img/DissolvedCarbonDioxide-n.png',
];

var TargetExtrs = ['°C',
	'%',
	'°C',
	'%',
	'mS/cm',
	'ph',
	'm/s',
	'°',
	'klux',
	'%'
];

function getIconByTarget(targetName) {
	var pos = TargetTypes.indexOf(targetName);

	if(pos > -1) {
		return TargetIcons[pos];
	} else {
		return TargetIcons[0];
	}
}

function getExtraByTarget(targetName) {
	var pos = TargetTypes.indexOf(targetName);

	if(pos > -1) {
		return TargetExtrs[pos];
	} else {
		return "";
	}
}

function getLastDay(year, month) {
	var d = new Date(0);
	if(month == 12) {
		d.setUTCFullYear(year + 1);
		d.setUTCMonth(0);
	} else {
		d.setUTCFullYear(year);
		d.setUTCMonth(month);
	}
	d.setTime(d.getTime() - 1);
	return d.getUTCDate();
}